<template>
  <el-dialog
    :title="!dataForm.brokerId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人手机号" prop="brokerPhone">
      <el-input v-model="dataForm.brokerPhone" placeholder="经纪人手机号"></el-input>
    </el-form-item>
    <el-form-item label="经纪人名称" prop="brokerName">
      <el-input v-model="dataForm.brokerName" placeholder="经纪人名称"></el-input>
    </el-form-item>
    <el-form-item label="经纪人密码" prop="brokerPassword">
      <el-input v-model="dataForm.brokerPassword" placeholder="经纪人密码"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="brokerImg">
      <el-input v-model="dataForm.brokerImg" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="brokerSex">
      <el-input v-model="dataForm.brokerSex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="brokerAddrSid">
      <el-input v-model="dataForm.brokerAddrSid" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="brokerAddrCid">
      <el-input v-model="dataForm.brokerAddrCid" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="brokerAddrQid">
      <el-input v-model="dataForm.brokerAddrQid" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="主营业务" prop="brokerBusiness">
      <el-input v-model="dataForm.brokerBusiness" placeholder="主营业务"></el-input>
    </el-form-item>
    <el-form-item label="公司描述" prop="brokerContent">
      <el-input v-model="dataForm.brokerContent" placeholder="公司描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          brokerId: 0,
          brokerPhone: '',
          brokerName: '',
          brokerPassword: '',
          brokerImg: '',
          brokerSex: '',
          brokerAddrSid: '',
          brokerAddrCid: '',
          brokerAddrQid: '',
          brokerBusiness: '',
          brokerContent: ''
        },
        dataRule: {
          brokerPhone: [
            { required: true, message: '经纪人手机号不能为空', trigger: 'blur' }
          ],
          brokerName: [
            { required: true, message: '经纪人名称不能为空', trigger: 'blur' }
          ],
          brokerPassword: [
            { required: true, message: '经纪人密码不能为空', trigger: 'blur' }
          ],
          brokerImg: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          brokerSex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          brokerAddrSid: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          brokerAddrCid: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          brokerAddrQid: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          brokerBusiness: [
            { required: true, message: '主营业务不能为空', trigger: 'blur' }
          ],
          brokerContent: [
            { required: true, message: '公司描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.brokerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.brokerId) {
            this.$http({
              url: this.$http.adornUrl(`/user/broker/info/${this.dataForm.brokerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.brokerPhone = data.broker.brokerPhone
                this.dataForm.brokerName = data.broker.brokerName
                this.dataForm.brokerPassword = data.broker.brokerPassword
                this.dataForm.brokerImg = data.broker.brokerImg
                this.dataForm.brokerSex = data.broker.brokerSex
                this.dataForm.brokerAddrSid = data.broker.brokerAddrSid
                this.dataForm.brokerAddrCid = data.broker.brokerAddrCid
                this.dataForm.brokerAddrQid = data.broker.brokerAddrQid
                this.dataForm.brokerBusiness = data.broker.brokerBusiness
                this.dataForm.brokerContent = data.broker.brokerContent
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/broker/${!this.dataForm.brokerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'brokerId': this.dataForm.brokerId || undefined,
                'brokerPhone': this.dataForm.brokerPhone,
                'brokerName': this.dataForm.brokerName,
                'brokerPassword': this.dataForm.brokerPassword,
                'brokerImg': this.dataForm.brokerImg,
                'brokerSex': this.dataForm.brokerSex,
                'brokerAddrSid': this.dataForm.brokerAddrSid,
                'brokerAddrCid': this.dataForm.brokerAddrCid,
                'brokerAddrQid': this.dataForm.brokerAddrQid,
                'brokerBusiness': this.dataForm.brokerBusiness,
                'brokerContent': this.dataForm.brokerContent
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
